package com.company.lagou.homework.homework3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ServerThread extends Thread{
    private Socket accept = null;
    private static Map<String, PrintWriter> userPws = new HashMap<>();

    public ServerThread(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        try {
            PrintWriter pw = new PrintWriter(accept.getOutputStream());
            pw.println("欢迎来到拉勾在线聊天系统，请输入您的昵称：");
            pw.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
            //读取客户端的消息
            String  userName = br.readLine();
            userPws.put(userName,pw);
            Set<String> userNames = userPws.keySet();
            for (int i = 0; i < userNames.size(); i++) {
                System.out.println("当前服务器已有用户："+userNames);
            }
            while (true) {
                String readStr = br.readLine();
                System.out.println("服务器接到客户端："+readStr);
                //将客户端发来的消息发送给客户端----需要改成群发
//                pw.println(readStr);
//                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
